<?php
/**
 * @author Feng
 * @date 2012-6-1
 */
class picture_photo_localeConcept extends system_concept_abstract {

	protected $_lang = null;
	public function __construct($lang = null) {
		$this->_lang = $lang;
		parent::__construct(array(), array());
	}
	
	public function init() {
		$this->setRowClass('picture_photo_row');
		$this->setModel('picture_photo_model', 'photo');
		$this->setModelCols('photo', array('id', 'hash', 'display_order', 'is_active', 'is_recommend', 'creationtime', 'lastmodified'));
	}
	
	public function save(picture_photo_row $row, $existingCheck = true) {
		if(!$row->getHash()) {
			throw new resource_exception("PHOTO-HASH Code ERROR.");
		}
		if(!preg_match('/^[0-9a-z]{32}$/is', $row->getHash())) {
			throw new resource_exception("PHOTO-HASH Code ERROR.");
		}	
		$row->set('display_order', intval($row->getDisplayOrder()));
		$row->setIsActive(($row->get('is_active') ? 1 : 0));
		$row->setIsRecommend(($row->get('is_recommend') ? 1 : 0));

		/**
		 * 因为special ID 有变化，所以这里要先取到locale的完事row
		 * 不可以修改顺序
		 */
		$locale = $row->getLocale();
		if(!$locale->getTitle()) {
			throw new picture_exception("Title can not be empty.");
		}
		if($row->getId()) {
			if($this->getModel('photo')->fetchRow(array('id != ?' => $row->getId(), 'hash = ?' => $row->getHash())) && $existingCheck) {
				throw new picture_exception("This Photo is already Existing.");
			}
			$row->setLastmodified(system_date::timestamp());
			$this->getModel('photo')->update($row->getModelData('photo'), array('id = ?' => $row->getId()));
		} else {
			if($existingCheck && $this->getModel('photo')->fetchRow(array('hash = ?' => $row->getHash()))) {
				throw new picture_exception("This Photo is already Existing.");
			}
			$row->setLastmodified(system_date::timestamp());
			$row->setCreationtime(system_date::timestamp());
			$id = $this->getModel('photo')->insert($row->getModelData('photo'));
			$row->setId($id);
		}
		/**
		 * 用前面取到的 locale row
		 */
		$locale->setPhotoId($row->getId());
		$locale->save();		
		$kConcept = new resource_keyword_concept();
		$pkConcept = new picture_photo_keyword_concept();
		$pkModel = new picture_photo_keyword_model();
		$pkModel->delete(array('photo_id = ?' => $row->getId()));
		foreach($row->getKeywords() AS $keyword) {
			if(!$keyword) continue;
			if(!($kRow = $kConcept->fetch(array('name = ?' => $keyword)))) {
				$kRow = $kConcept->create();
				$kRow->setIsActive(1);
				$kRow->setName($keyword);
				$kRow = $kRow->save();
			}				
			$pkRow = $pkConcept->create();
			$pkRow->setPhotoId($row->getId());
			$pkRow->setKeywordId($kRow->getId());
			$pkRow->save();
		}		
		return $row;
	}

	protected $_locale = null;
	public function getLocale($row, $lang = null) {
		if($lang !== null) $this->_lang = $lang;
		if($this->_locale === null || $this->_locale->getPhotoId() != $row->getId()) {
			$concept = new picture_photo_locale_concept();
			if(!($this->_locale = $concept->fetch(array('photo_id = ?' => intval($row->getId()), 'lang = ?' => $this->_lang)))) {
				$this->_locale = $concept->create();
				$this->_locale->setLang($this->_lang);
				$this->_locale->setPhotoId(intval($row->getId()));
			}
		}
		return $this->_locale;
	}
	
	public function delete(picture_photo_row $row, $options = array()) {
		$id = $row->getId();
		$db = system_db::getAdapter();
		$where = "`photo_id` = '$id'";
		if(isset($options['locale_only'])) {
			$where .= " AND `lang` = '{$this->_lang}'";
		}
		$db->query("DELETE FROM `picture_photo_locale` WHERE $where");
		$db->query("DELETE FROM `picture_photo_keyword` WHERE `photo_id` = '$id'");
		$db->query("DELETE FROM `picture_special_photo` WHERE `photo_id` = '$id'");
		if(!isset($options['reserve_collection'])) {
			$db->query("DELETE FROM `collection_photo` WHERE `link` IN(SELECT `link_key` FROM `picture_photo_collection` WHERE `photo_id` = '$id')");
			$db->query("DELETE FROM `picture_photo_collection` WHERE `photo_id` = '$id'");
		}
		
		if(!isset($options['locale_only'])) {
			$db->query("DELETE FROM `picture_photo` WHERE `id` = '$id'");
			$db->query("DELETE FROM `picture_photo_collection` WHERE `photo_id` = '$id'");
		}
		
		$sid = array();
		$model = new picture_special_photo_model();
		foreach($model->fetchAll("`photo_id` = '$id'") AS $sp) {
			$sid[$sp['special_id']] = $sp['special_id'];
		}
		$model->delete("`photo_id` = '$id'");
		if(!empty($sid)) {
			$sid = implode(',', $sid);
			$concept = new picture_special_localeConcept();
			foreach($concept->fetchAll(array("special.id IN($sid)")) AS $special) {
				$special->updatePhotoCount();
			}
		}
	}

}
